﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using JPW.OnlinePaperSubmissionSystem.Project.Dto;
using Kendo.DynamicLinq;
using System.Data.Entity;
namespace JPW.OnlinePaperSubmissionSystem.Project
{
    
   // [AbpAuthorize(AppPermissions.Pages_Tenant_Files)]
    public class FilesAppService : AbpZeroTemplateAppServiceBase, IFilesAppService
    {
        private readonly IRepository<Files,long> _filesRepository;
        public FilesAppService(IRepository<Files,long> filesRepository)
        {
            _filesRepository = filesRepository;
            
        }
        
        public DataSourceResult KendoGetFiless(DataSourceRequest input)
        {
         var filess = _filesRepository
            .GetAll()
            .OrderBy(p => p.Id)
            .MapTo<List<FilesListDto>>().AsQueryable()
            .ToDataSourceResult(input);

           return filess;
        }
         public ListResultOutput<FilesListDto> GetFiless(GetFilesInput input)
        {
            var filess = _filesRepository
            .GetAll()
            //.WhereIf(
            //    !input.Filter.IsNullOrEmpty(),
            //    p => p.Name.Contains(input.Filter) ||
            //            p.Code.Contains(input.Filter) 
                        //||p.Performance==Convert.ToInt32(input.Filter)
            //)
            .OrderBy(p => p.Id)
            .ToList();

            return new ListResultOutput<FilesListDto>(filess.MapTo<List<FilesListDto>>());
        }
         public async Task<ListResultOutput<FilesListDto>> GetFilessAsync()
        {
            var query = await _filesRepository.GetAll().OrderBy(p => p.Id).ToListAsync();

            return new ListResultOutput<FilesListDto>(query.MapTo<List<FilesListDto>>());

        }
        
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Files_Delete)]
        public async Task DeleteFiles(IdInput input)
        {
            await _filesRepository.DeleteAsync(input.Id);
        }
        
        public async Task CreateOrUpdateFiles(CreateOrUpdateFilesInput input)
        {
            if (input.Files.ID.HasValue)
            {
                await UpdateFilesAsync(input);
            }
            else
            {
                await CreateFilesAsync(input);
            }
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Files_Create)]
        public async Task CreateFilesAsync(CreateOrUpdateFilesInput input)
        {
            var files = input.Files.MapTo<Files>();
            await _filesRepository.InsertAsync(files);
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Files_Update)]
        public async Task UpdateFilesAsync(CreateOrUpdateFilesInput input)
        {
            var files = input.Files.MapTo<Files>();
            await _filesRepository.UpdateAsync(files);
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Files_Update)]
        public async Task<GetFilesForEditOutput> GetFilesForEdit(NullableIdInput input)
        {
            Files files = null;
            if (input.Id.HasValue) //Editing existing role?
            {
                files = await _filesRepository.FirstOrDefaultAsync(input.Id.Value);
            }
            return new GetFilesForEditOutput
            {
                Files = files.MapTo<FilesEditDto>(),
            };
        }
        public long InsertAndGetId(Files input) {
            return _filesRepository.InsertAndGetId(input);
        }
        public Files GetFileById(long input)
        {
           return  _filesRepository.FirstOrDefault(x=>x.Id==input);
        }
    }
}

